package com.bw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.ZkList;
import com.bw.entity.ZkShopping;
import com.bw.service.IZkListService;
import com.bw.service.IZkUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author szg
 * @since 2020-05-27
 */
@RestController
@CrossOrigin
@RequestMapping("/list")
public class ZkListController {

    @Autowired
    private IZkListService listService;

    @GetMapping("/getlist")
    public List<ZkList> getlist(){
        System.out.println("展示列表");
        return listService.list();
    }
    @PostMapping("/up/{zt}/{id}")
    public String up(@PathVariable Integer zt,@PathVariable Integer id){
        System.out.println("xiugaizt");
        ZkList l1 = listService.getById(id);
        l1.setZt(1);
        listService.saveOrUpdate(l1);
        return "修改成功";
    }

    @PostMapping("/add")
    public String add(@RequestBody ZkList list){
        System.out.println(list.toString());
        list.setBh(this.getRundown());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sid",list.getSid());
        List<ZkList> list1 = listService.list(queryWrapper);
        int n1=0;
        if (list1!=null){
            for (ZkList l:list1
                 ) {
                n1=l.getNum();
                if (l.getNum()>=10){
                    return "只能采购库存量低于10的商品";
                }
                break;
            }
        }
        listService.save(list);
        return "添加成功";
    }
    public String getRundown(){
        String num="";
            int sjs = new Random().nextInt(999999);
            if(sjs<100000){
                sjs+=100000;
            }
            num+=sjs;

        System.out.println(num);
        return num;
    }
}
